<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { timeFormat, timeFormat_2, toYuan, copyToCliboard, getNowDay } from '@/hooks/util'
import { message } from 'ant-design-vue'
import * as orderApi from '@/api/order'
import * as accountApi from '@/api/account'
import * as commerceOrderApi from '@/api/commerceOrder'
import SearchDrawer from '@/components/search-drawer/Index.vue'

const searchDrawerRef = ref()

const route = useRoute()

let searchFlag = ref<boolean>(false)

interface Pagination {
  page: number
  pageSize: number
  total: number
  current?: number
  onChange: Function
  showSizeChanger: boolean
  showQuickJumper: boolean
}

const handlePageChange = (page: any, pageSize: any) => {
  // console.log(page, pageSize)
  pagination.value.pageSize = pageSize
  searchParams.value.pageSize = pageSize
}

const pagination = ref<Pagination>({
  page: 1,
  pageSize: 10,
  total: 0,
  onChange: handlePageChange,
  showSizeChanger: true,
  showQuickJumper: true,
})
let loading = ref<boolean>(false)

interface SearchParams {
  currentPage: number
  pageSize: number
  [propName: string]: any
}
const searchParams = ref<SearchParams>({
  currentPage: pagination.value.page,
  pageSize: pagination.value.pageSize,
})

const dataSource = ref([])
const columns = [
  {
    title: '渠道信息',
    key: 'channelData',
    width: 500,
  },
  {
    title: '平台信息',
    key: 'planetForm',
    width: 300,
  },
  {
    title: '商品信息',
    key: 'productInfo',
    width: 400,
  },

  {
    title: '状态信息',
    key: 'orderStatus',
    width: 300,
  },
  {
    title: '资金信息',
    key: 'finalPrice',
    width: 300,
  },

  {
    title: '时间信息',
    key: 'createTime',
    width: 300,
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    fixed: 'right',
  },
]

// 翻页
const changePage = (e: any) => {
  pagination.value.current = e.current
  pagination.value.pageSize = e.pageSize
  searchParams.value.currentPage = pagination.value.current as number
  getList()
}

// 获取当前列表
const getList = async () => {
  loading.value = true
  try {
    let { state, data, message: msg } = await commerceOrderApi.OrderList(searchParams.value)
    if (state === 200) {
      dataSource.value = data.list
      pagination.value.total = Number(data.totalCount)
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
  loading.value = false
}

// 翻译渠道
const getChannel = (num: string | number) => {
  if (num == 1) {
    return '微信'
  } else if (num == 2) {
    return '企业微信'
  } else if (num == 3) {
    return '闲鱼'
  }
}

onMounted(() => getList())

import type { SelectProps } from 'ant-design-vue'
const NumberType = ref(1)


const orderNumberType = ref<SelectProps['options']>([
  {
    label: '闲鱼单号',
    value: 1,
  },
  {
    label: '平台单号',
    value: 2,
  },
  {
    label: '渠道单号',
    value: 3,
  },
])

const orderStatusList = ref<SelectProps['options']>([
  {
    label: '待付款',
    value: 1,
  },
  {
    label: '待发货',
    value: 2,
  },
  {
    label: '已发货',
    value: 3,
  },
  {
    label: '交易完成',
    value: 4,
  },
  {
    label: '交易关闭',
    value: 5,
  },
  {
    label: '退货',
    value: 6,
  },
])

const formRef = ref()
interface FormState {
  fansNickName?: string
  productTitle?: string
  orderStatus?: string | number
  channelOrderNumber?: string
  fishOrderNumber?: string
  orderNumber?: string
  numberValue?: string //单号

}
const formState = reactive<FormState>({})



const numberTitle = ref("闲鱼单号")

const changeNumberType = (e) => {
  console.log(e, "e");
  if (e == 1) {
    numberTitle.value = "闲鱼单号"
  } else if (e == 2) {
    numberTitle.value = "平台单号"
  } else if (e == 3) {
    numberTitle.value = "渠道单号"
  }
}
const itemInfo = ref({}) // 点击支付出票，数据
// 查询
const search = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.validate().then(() => {
    searchParams.value.productTitle = formState.productTitle || ''
    searchParams.value.orderStatus = formState.orderStatus || ''
    searchParams.value.channelOrderNumber = formState.channelOrderNumber || ''
    searchParams.value.fishOrderNumber = formState.fishOrderNumber || ''
    searchParams.value.orderNumber = formState.orderNumber || ''
    searchParams.value.fansNickName = formState.fansNickName || ''

    if (NumberType.value == 1) {
      searchParams.value.fishOrderNumber = formState.numberValue || ''
    } else if (NumberType.value == 2) {
      searchParams.value.orderNumber = formState.numberValue || ''
    } else if (NumberType.value == 3) {
      searchParams.value.channelOrderNumber = formState.numberValue || ''
    }
    formState.numberValue = ''


    getList()
  })
}

// 重置
const reset = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.resetFields()

  formRef.value.resetFields()
  for (let key in searchParams.value) {
    if (key != 'currentPage' && key != 'pageSize' && key != 'robotId')
      searchParams.value.hasOwnProperty(key) && delete searchParams.value[key]
  }
  NumberType.value = 1

  getList()
}

const handleViewLogistics = (record: any) => {
  window.open(`https://www.baidu.com/s?word=${record.trackingNumber}`)
}




</script>

<template>
  <page-container :title="route.meta.title">
    <a-card style="overflow-x: scroll">
      <div class="searchBox">
        <a-form ref="formRef" layout="inline" :model="formState" class="formClass">
          <a-form-item label="单号方式" name="NumberType">
            <a-select v-model:value="NumberType" placeholder="单号方式" @change="changeNumberType" style="width: 250px"
              allowClear>
              <a-select-option v-for="(item, index) in orderNumberType" :key="index" :value="item.value">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item :label="numberTitle" name="fansNickName">
            <a-input v-model:value="formState.numberValue" :placeholder="numberTitle" style="width: 200px" />
          </a-form-item>

          <a-form-item label="买家昵称" name="fansNickName">
            <a-input v-model:value="formState.fansNickName" placeholder="买家昵称" style="width: 200px" />
          </a-form-item>
          <!-- orderStatusList -->
          <a-form-item label="商品标题" name="productTitle">
            <a-input v-model:value="formState.productTitle" placeholder="商品标题" style="width: 200px" />
          </a-form-item>

          <a-form-item label="订单状态" name="orderStatus">
            <a-select v-model:value="formState.orderStatus" placeholder="订单状态" style="width: 250px" allowClear>
              <a-select-option v-for="(item, index) in orderStatusList" :key="index" :value="item.value">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="search">搜索</a-button>
            <a-button @click="reset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>

      <a-alert description="小提示：只要鼠标在表格上，按住左Shift键并滚动鼠标滚轮就可以横向滚动表格" type="info" banner
        style="margin-bottom: 10px; padding: 10px" />

      <a-table style="overflow-x: scroll" :columns="columns" :data-source="dataSource" :pagination="pagination"
        :loading="loading" @change="changePage" rowKey="id" :scroll="{ x: 100 }" size="small">
        <template #emptyText>
          <a-empty description="暂无数据" />
        </template>

        <template #headerCell="{ column }">
          <template v-if="column.key === 'id'">
            <span>id</span>
          </template>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'productInfo'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="商品主图">

                <a-image :width="30" :src="record?.productImg" />

              </a-descriptions-item>
              <a-descriptions-item label="商品标题">
                <a-tooltip placement="top">
                  <template #title>
                    {{ record.productTitle || '-' }}
                  </template>
                  <span class="hiddenText">{{ record.productTitle || '-' }}</span>
                </a-tooltip>
              </a-descriptions-item>
              <a-descriptions-item label="商品类目">{{ record.productCateName || '-' }}</a-descriptions-item>

            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'finalPrice'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="实付金额">
                ￥{{ toYuan(record.finalPrice) || '-' }}

              </a-descriptions-item>
              <a-descriptions-item label="商品总价">
                ￥{{ toYuan(record.productPriceAll) || '-' }}

              </a-descriptions-item>
              <a-descriptions-item label="返佣">
                ￥{{ toYuan(record.shopProfit) || '-' }}

              </a-descriptions-item>


            </a-descriptions>
          </template>



          <template v-else-if="column.key === 'channelData'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="平台单号">
                {{ record.orderNumber }}
                <a @click="copyToCliboard(record.orderNumber)">
                  <CopyOutlined />
                </a>
              </a-descriptions-item>
              <a-descriptions-item label="渠道单号">
                {{ record.channelOrderNumber || '-' }}
                <a v-if="record.channelOrderNumber != ''" @click="copyToCliboard(record.channelOrderNumber)">
                  <CopyOutlined />
                </a>
              </a-descriptions-item>
              <a-descriptions-item label="闲鱼单号">
                {{ record.fishOrderNumber || '-' }}
                <a v-if="record.fishOrderNumber != ''" @click="copyToCliboard(record.fishOrderNumber)">
                  <CopyOutlined />
                </a>
              </a-descriptions-item>
            </a-descriptions>
          </template>
          <template v-else-if="column.key === 'planetForm'">
            <div>
              <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
                <a-descriptions-item label="推广平台">
                  {{ getChannel(record.channel) || '-' }}
                </a-descriptions-item>
                <a-descriptions-item label="平台账号">
                  {{ record.robotNickName || '-' }}
                </a-descriptions-item>

                <a-descriptions-item label="账号编号">
                  {{ record.robotId || '-' }}
                </a-descriptions-item>
              </a-descriptions>
            </div>
          </template>

          <template v-else-if="column.key === 'orderStatus'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="支付状态">
                <a-tag v-if="record.payStatus == -1" color="red">已退款</a-tag>
                <a-tag v-if="record.payStatus == 2" color="red">已付款</a-tag>
                <a-tag v-if="record.payStatus == 1" color="green">待付款</a-tag>
              </a-descriptions-item>
              <a-descriptions-item label="订单状态">
                <a-tag v-if="record.orderStatus == 1" color="green">待付款</a-tag>
                <a-tag v-if="record.orderStatus == 2" color="blue">待发货</a-tag>
                <a-tag v-if="record.orderStatus == 3" color="green">已发货</a-tag>
                <a-tag v-if="record.orderStatus == 4" color="green">交易完成</a-tag>
                <a-tag v-if="record.orderStatus == 5" color="red">交易关闭</a-tag>
                <a-tag v-if="record.orderStatus == 6" color="red">退货</a-tag>

              </a-descriptions-item>
              <a-descriptions-item label="快递单号">
                {{ record.trackingNumber || '-' }}

              </a-descriptions-item>

            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'action'">
            <div>
              <a-button :disabled="true" type="link">拦截订单</a-button>

            </div>
            <div>
              <a-button :disabled="record.orderStatus != 3" @click="handleViewLogistics(record)"
                type="link">查看物流</a-button>

            </div>

          </template>

          <template v-else-if="column.key === 'createTime'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="创建时间">
                {{ timeFormat_2(record.orderCreateTime) || '-' }}
              </a-descriptions-item>

              <a-descriptions-item label="付款时间">
                {{ timeFormat_2(record.payTime) || '-' }}
              </a-descriptions-item>


              <a-descriptions-item label="发货时间" v-if="record.orderStatus == 3">
                {{ timeFormat_2(record.shipTime) || '-' }}
              </a-descriptions-item>

              <a-descriptions-item label="退款时间" v-if="record.orderStatus == 6">
                {{ timeFormat_2(record.refundTime) || '-' }}
              </a-descriptions-item>

              <a-descriptions-item label="结算时间" v-if="record.orderStatus == 4">
                {{ timeFormat_2(record.settleTime) || '-' }}
              </a-descriptions-item>
            </a-descriptions>
          </template>
        </template>
      </a-table>
    </a-card>





  </page-container>
</template>

<style lang="less" scoped>
.hiddenText {
  display: inline-block;
  width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  line-height: 16px;
}

//支付出票弹框的样式
.pay-modal {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;

  .info-data {
    // width: 445px;
    padding: 4px 20px;
    // background-color: aqua;
    height: 395px;
  }

  .channel-box {
    display: flex;
    flex-direction: column;
    flex: 1;
  }

  .limit-box {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    margin: 15px 0;
    padding-left: 12px;

    .left {
      .title {
        font-size: 14px;
        font-weight: bold;
        margin-left: 5px;
      }

      .value {
        margin-left: 15px;
        font-size: 14px;
        font-weight: bold;
        color: #ed1a23;
      }
    }
  }
}

.tan-He {
  width: 100%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;

  .tan-Header {
    width: 100%;
    margin: 10px 10px;
    padding-bottom: 8px;
    border-bottom: 1.5px solid #f5f5f5;
    font-weight: bold;
    font-size: 14px;
  }
}

.aTag-seatNo:nth-child(n + 2) {
  margin-top: 3px;
}

.formClass {
  :nth-child(1) {
    margin-bottom: 2px;
  }
}

.searchBox {
  margin-bottom: 10px;
  display: flex;

  input {
    width: 200px;
    margin-right: 10px;
  }

  button {
    margin-right: 10px;
  }
}

a:hover {
  color: red;
}

.imgBox {
  width: 50px;
  height: 50px;

  img {
    height: 50px;
  }
}

.popoverImg {
  width: 300px;

  img {
    width: 100%;
  }
}

.robitStatusClass {
  text-align: center;
}

:deep(.ant-form-item-label) {
  width: 70px;
}
</style>
